﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Hello_Halcon
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 用来存放后面所有新增的锚点
        /// </summary>
        private List<HDrawingObject_CircleModel> drawingObjects;
        public MainWindow()
        {
            drawingObjects = new List<HDrawingObject_CircleModel>();


            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //添加文件路径
            string fileName = "D:\\workspace\\program\\Halcon\\Images\\1.png";

            var image = new HImage(fileName);

            int width, height;
            image.GetImageSize(out width, out height);

            //hSmart.HalconWindow.SetPart(0, 0, width, height);
            hSmart.HalconWindow.DispObj(image);
            //hSmart.SetFullImagePart();
            //hSmart.SetFullImagePart();

        }


        /// <summary>
        /// 画圆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            //创建一个圆形，圆心为(100,100)，半径为50
            var drawingObject = HDrawingObject.
                CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, new HTuple[] { 100, 100, 50 });

            //----添加回调---
            //添加拖拽回调
            drawingObject.OnDrag(HDrawingObjectCallbackClass);
            //放缩变化
            drawingObject.OnResize(HDrawingObjectCallbackClass);

            var model = new HDrawingObject_CircleModel(drawingObject);
            //临时存放List
            drawingObjects.Add(model);
            model.Print();

            //将圆画再hSmart画布上面
            hSmart.HalconWindow.AttachDrawingObjectToWindow(drawingObject);


        }

        /// <summary>
        /// 回调函数
        /// </summary>
        /// <param name="drawingObject">回调物体</param>
        /// <param name="window">窗体</param>
        /// <param name="type">类型</param>
        public void HDrawingObjectCallbackClass(HDrawingObject drawid, HWindow window, string type)
        {

            var drawObj = drawingObjects.FirstOrDefault(t => t.HDrawingObject.ID.Equals(drawid.ID));

            //如果不为空则打印
            if (drawObj != null)
            {
                drawObj.Update(drawid);
                drawObj.Print();
            }
            else
            {
                Debug.WriteLine($"drawid.id[{drawid.ID} is not find!]");
            }

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            TemplateService templateService = new TemplateService();
            templateService.Action();
        }

        private void hSmart_Loaded(object sender, RoutedEventArgs e)
        {
            hSmart.SetFullImagePart();

        }
    }

    /// <summary>
    /// 因为HDrawingObject没有坐标参数，所以我们为了方便操作添加创建了一个HDrawingObjectModel
    /// </summary>
    public class HDrawingObject_CircleModel
    {
        public HTuple? Row { get; set; }

        public HTuple? Column { get; set; }

        public HTuple? Radius { get; set; }

        public readonly HDrawingObject HDrawingObject;

        public readonly long Id;

        public HDrawingObject_CircleModel(HDrawingObject hDrawingObject)
        {
            HDrawingObject = hDrawingObject;
            Id = HDrawingObject.ID;

            Update(HDrawingObject);
        }



        public void Print()
        {
            Debug.WriteLine($"id:[{HDrawingObject.ID}],row:[{Row}],column:[{Column}],radius:[{Radius}]");

        }

        public void Update(HDrawingObject hDrawingObject)
        {
            Row = hDrawingObject.GetDrawingObjectParams("row");
            Column = hDrawingObject.GetDrawingObjectParams("column");
            Radius = hDrawingObject.GetDrawingObjectParams("radius");
        }

    }
}
